package com.qyer.search.engine;

import static com.qyer.log.WarningLogger.logWarning;
import static com.qyer.search.model.lastminute.SKU.unknownPositionSKU;

import com.qyer.search.model.SearchParameterOrigin;
import com.qyer.search.model.SecondComparableTuple2;
import com.qyer.search.model.lastminute.SKU;
import com.qyer.search.model.result.Type1SearchPackage;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.RecursiveTask;
import com.qyer.search.model.middle.*;

/**
 * Created by tangzheng on 16/5/17.
 */
public class RecursiveSortTask extends RecursiveTask<RecursiveSortResult> {

  public static int THRESHOLD = 100;

  private List<SKU> skus;
  private int start;
  private int end;
  //private List<SKU> unknownList;
  //private List<SecondComparableTuple2<SKU, Integer>> tuple2List;
  private Map<Integer, Integer> positionMap;
  private SearchParameterOrigin sp;
  private int type;
  public RecursiveSortTask(List<SKU> skus, Map<Integer,Integer> positionMap, SearchParameterOrigin sp,
                           int type){
    this.skus = skus;
    this.positionMap = positionMap;
    this.sp = sp;
    this.type = type;
  }

  public RecursiveSortTask(List<SKU> skus, int start, int end, Map<Integer,Integer> positionMap,
                           SearchParameterOrigin sp, int type) {
    this.skus = skus;
    this.start = start;
    this.end = end;
    this.positionMap = positionMap;
    this.sp = sp;
    this.type = type;
  }

  @Override public RecursiveSortResult compute(){
    long t1 = System.currentTimeMillis();
    if((end - start) > THRESHOLD){
      int middle = (start + end) / 2;
      RecursiveSortTask left = new RecursiveSortTask(skus, start, middle, positionMap, sp, type);
      RecursiveSortTask right = new RecursiveSortTask(skus, middle, end, positionMap, sp, type);

      left.fork();
      right.fork();
      //System.out.println(type+":时A间:"+(System.currentTimeMillis() - t1)+":size:"+(end - start));
      return new RecursiveSortResult(left.join(), right.join());

    }else {
      List<SKU> unknownList = new ArrayList<>();
      List<SecondComparableTuple2<SKU, Integer>> tuple2List = new ArrayList<>(skus.size());
      long t11 = System.currentTimeMillis();
      for (int i = start; i < end; i++) {
        SKU sku = skus.get(i);
        int lid = sku.getId();
        if(positionMap.containsKey(lid)){
          tuple2List.add(new SecondComparableTuple2<>(sku, positionMap.get(lid)));
          logWarning(unknownPositionSKU(lid), sp, null);
        }else {
          unknownList.add(sku);
        }
        /*
        int position = positionMap.get(lid);
        // 获取位置信息, 放到SecondComparableTuple2准备按二元组的第二元素排序, 不认识的商品不参与排序
        if (position == null) {
          logWarning(unknownPositionSKU(lid), sp, null);
          unknownList.add(sku);
        } else {
          tuple2List.add(new SecondComparableTuple2<>(sku, position));
        }*/
      }
      //System.out.println(type+":时B间:"+(System.currentTimeMillis() - t1)+":size:"+(end - start));
      return new RecursiveSortResult(unknownList, tuple2List, type);
    }
  }
}
